import 'package:aitao_flutter/common_ui/back_nav.dart';
import 'package:aitao_flutter/common_ui/confirm_btn.dart';
import 'package:aitao_flutter/style/box_style.dart';
import 'package:aitao_flutter/style/color_style.dart';
import 'package:aitao_flutter/style/edgeInsets_style.dart';
import 'package:aitao_flutter/style/text_style.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../request/api.dart';
import '../../request/requestManager.dart';

class BingAccountPage extends StatefulWidget {
  const BingAccountPage({super.key});

  @override
  State<BingAccountPage> createState() => _BingAccountPageState();
}

class _BingAccountPageState extends State<BingAccountPage> {
  TextEditingController accountController = TextEditingController();
  TextEditingController nameController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BackNavigation(
        title: '账号绑定',
      ),
      body: Container(
        padding: EdgeInsetsStyle.only(left: 40, right: 40),
        color: ColorStyle.whiteColor,
        child: Column(children: [
          inputWidget(0),
          inputWidget(1),
          Box.vBox(100),
          const Text(
            '请认真核对，否则提现失败',
            style: TextStyle(fontSize: 14, color: ColorStyle.color777777),
          ),
          Box.vBox30,
          ConfirmButton(
            title: '保存',
            backGroundColor: ColorStyle.themeColor,
            titleColor: Colors.white,
            borderRaduis: 8,
            callBack: () {
              if (accountController.text.isEmpty) {
                EasyLoading.showToast('请输入账号');
                return;
              }
              if (nameController.text.isEmpty) {
                EasyLoading.showToast('请输入姓名');
                return;
              }
              saveBindAccount();
            },
          )
        ]),
      ),
    );
  }

  Widget inputWidget(int index) {
    return Container(
      margin: EdgeInsetsStyle.only(top: 10),
      child: Row(children: [
        Text(
          index == 0 ? '支付宝账号:' : '真实姓名:',
          style: Styles.normalText_16,
        ),
        Box.hBox15,
        Expanded(
            child: TextField(
          controller: index == 0 ? accountController : nameController,
          decoration: InputDecoration(
              border: InputBorder.none,
              hintText: index == 0 ? '请输入账号' : '请输入姓名',
              hintStyle:
                  const TextStyle(color: ColorStyle.color777777, fontSize: 14)),
        ))
      ]),
    );
  }

  @override
  void initState() {
    super.initState();
    getBingAli();
  }

  void getBingAli() async {
    var result = await RequestManager().post(API.wallAlipay);
    if (result != null && result['code'] == 0) {
      setState(() {
        accountController.text = result['data']['card_account'];
        nameController.text = result['data']['card_name'];
      });
    }
  }

  void saveBindAccount() async {
    if (verifyInputContent() == '') {
      var result = await RequestManager().post(API.wallAliBind, params: {
        'card_account': accountController.text,
        'card_name': nameController.text
      });
      if (result != null && result['code'] == 0) {
        EasyLoading.showToast(result['message']);
        Get.back();
      }
    } else {
      EasyLoading.showToast(verifyInputContent());
    }
  }

  String verifyInputContent() {
    if (accountController.text.isEmpty) {
      return '请输入账号';
    }
    if (accountController.text.length > 30) {
      return '账号长度不超过30';
    }
    if (nameController.text.isEmpty) {
      return '请输入姓名';
    }
    if (accountController.text.length > 30) {
      return '账号长度不超过30';
    }
    return '';
  }
}
